import {
  postSmList,
  postMbList,
  postSmAreaEdit,
  postYbList,
  postCsList,
  postYbMachineList,
  postCsMachineList,
  postProductList,
  postAreaList,
  postTaskList,
  postSmEdit,
  postPointEdit,
  postYbEdit,
  postGroupList,
  postAreaEdit,
  postProductEdit,
  postDispatchList,
  postDispatchAdd,
  postDispatchDelete,
  postTaskareaEdit,
  postAreaPointEdit,
  postBreedList,
  postBatteryList,
  postBatteryEdit,
  postTaskDetail,
  postUserList
} from '../../service/api'
import { defineStore } from 'pinia'
import useSetStore from '../setting/setting'
const useMainStore = defineStore('main', {
  state: () => ({
    sm: [],
    yb: [],
    yb_machine: [],
    product: [],
    area: [],
    task: [],
    group: [],
    productName: '全部',
    breedName: '全部',
    ptList: [],
    breed: [],
    battery: [{ value: 0 }],
    taskDetailList: [],
    userForm: { username: '', password: '' },
    isShowForm: false,
    authenticated: false,
    userInfo: {}
  }),
  actions: {
    async getUserInfo() {
      const { data } = await postUserList()
      this.userInfo = data
    },
    setAuthenticated(value) {
      this.authenticated = value
    },
    setShowForm(value) {
      this.isShowForm = value
    },
    //初始加载页面就需要的数据
    async getSmList() {
      const setStore = useSetStore()
      let smResult = {
        data: []
      }
      if (setStore.setting.process == '并粗工序') {
        smResult = await postMbList()
      } else {
        smResult = await postSmList()
      }
      this.sm = smResult.data
    },
    async getSmEdit(data) {
      const smEditResult = await postSmEdit(data)
      this.getSmList()
    },
    async getSmAreaEdit(data) {
      const smEditResult = await postSmAreaEdit(data)
      this.getSmList()
    },
    async getGroupList() {
      const groupResult = await postGroupList()
      this.group = groupResult.data
    },

    async getYbList() {
      const setStore = useSetStore()
      let ybResult = {
        data: []
      }

      if (setStore.setting.process == '并粗工序') {
        ybResult = await postCsList()
      } else {
        console.log(setStore.setting.process)
        ybResult = await postYbList()
      }
      this.yb = ybResult.data
    },

    async getTaskDetail(data) {
      const taskDetailResult = await postTaskDetail(data)
      this.taskDetailList = taskDetailResult.data
    },
    async getYbEdit(data) {
      const YbEditResult = await postYbEdit(data)
      this.getYbList()
    },
    async getYbMchineList() {
      const setStore = useSetStore()
      let csResult = {
        data: []
      }
      if (setStore.setting.process == '并粗工序') {
        csResult = await await postCsMachineList()
      } else {
        csResult = await await postYbMachineList()
      }
      this.yb_machine = csResult.data
    },
    async getProductList() {
      const productResult = await postProductList()
      this.product = productResult.data
    },
    async getProductEdit(data) {
      const productEditResult = await postProductEdit(data)
      this.getProductList()
    },
    async getBreedList() {
      const BreedResult = await postBreedList()
      this.breed = BreedResult.data
    },
    async getAreaList() {
      const AreaResult = await postAreaList()
      this.area = AreaResult.data
    },
    async getAreaEdit(data) {
      const AreaEditResult = await postAreaEdit(data)
      this.getAreaList()
    },
    async getAreaPointEdit(data) {
      console.log(111)

      const AreaPointEditResult = await postAreaPointEdit(data)
      this.getSmList()
      this.getYbList()
      this.getAreaList()
    },
    async getTaskList() {
      const TaskResult = await postTaskList()
      this.task = TaskResult.data
    },
    async getPointEdit(data) {
      const pointEditResult = await postPointEdit(data)
      this.getSmList()
      this.getYbList()
      this.getAreaList()
    },
    changeProduct(data) {
      this.productName = data
    },
    changeBreed(data) {
      this.breedName = data
    },
    async getDispatchList() {
      const DispatchResult = await postDispatchList()
      this.ptList = DispatchResult.data
    },
    async getDispatchAdd(data) {
      const DispatchAddResult = await postDispatchAdd(data)
      this.getDispatchList()
    },
    async getDispatchDelete(data) {
      const DispatchDeleteResult = await postDispatchDelete(data)
      this.getDispatchList()
    },
    async getTaskareaEdit() {
      const DispatchDeleteResult = await postTaskareaEdit()
      this.getDispatchList()
    },
    async getBatteryList() {
      const BatteryResult = await postBatteryList()
      this.battery = BatteryResult.data
    },
    async getBatteryEdit(data) {
      const BatteryResult = await postBatteryEdit(data)
      this.getBatteryList()
    }
  }
})

export default useMainStore
